package com.github.tetrisgame.ui.activity;

import android.Manifest;
import android.animation.ObjectAnimator;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.provider.Settings;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.ActivityResultRegistry;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.github.tetrisgame.R;
import com.github.tetrisgame.databinding.ActivityLoginBinding;
import com.github.tetrisgame.ui.view.CircularImageView;
import com.github.tetrisgame.utils.PermissionsUtils;
import com.github.tetrisgame.utils.StringUtils;
import com.github.tetrisgame.utils.TextWatcherImpl;
import com.github.tetrisgame.utils.UIUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.UpdateListener;

public class LoginActivity extends AppCompatActivity {
    ActivityLoginBinding mBinding;
    private boolean isChecked = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        mBinding = ActivityLoginBinding.inflate(getLayoutInflater());
        setContentView(mBinding.getRoot());
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        mBinding.ivCheck.setOnClickListener(v -> {
            if (isChecked) {
                mBinding.ivCheck.setImageResource(R.drawable.ic_check_circle);
            } else {
                mBinding.ivCheck.setImageResource(R.drawable.ic_selected_circle);
            }
            isChecked = !isChecked;
        });

        //监听输入框文本发生改变的事件
        mBinding.etPhone.addTextChangedListener(new TextWatcherImpl() {
            //限制最多输入11个文本内容
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() < 11) {
                    mBinding.btnVerify.setEnabled(false);
                }

                if (s.toString().length() == 11) {
                    boolean isPhone = StringUtils.isPhone(s.toString());
                    if (isPhone) {
                        mBinding.btnVerify.setEnabled(true);
                    } else {
                        Toast.makeText(LoginActivity.this, "不是一个正确的手机号", Toast.LENGTH_SHORT).show();
                    }
                }

                if (s.toString().length() > 11) {
                    mBinding.etPhone.setText(s.subSequence(0, 11));
                    mBinding.etPhone.setSelection(11);
                }
            }

        });

        mBinding.btnVerify.setOnClickListener(v -> {
            if (!isChecked) {
                ObjectAnimator animator = ObjectAnimator.ofFloat(
                        mBinding.agreementContainer,
                        "x",
                        0, -UIUtils.dp2pxF(50), 0, UIUtils.dp2pxF(50), 0);
                animator.setDuration(200);
                animator.setRepeatCount(4);
                animator.setRepeatMode(ObjectAnimator.RESTART);
                animator.start();
                return;
            }
            //发送验证码
            BmobSMS.requestSMSCode(mBinding.etPhone.getText().toString(), "DataSDK", new QueryListener<Integer>() {
                @Override
                public void done(Integer smsId, BmobException e) {
                    if (e == null) {
                        //发送成功
                        Intent intent = new Intent(LoginActivity.this, VerifyActivity.class);
                        startActivity(intent);
                    } else {
                        Toast.makeText(LoginActivity.this, "发送验证码失败", Toast.LENGTH_LONG).show();
                        e.printStackTrace();
                    }
                }
            });
        });

        //手动配置视图的属性
        /*
           mBinding.headView.setBorderWidth(UIUtils.dp2px(10));
           mBinding.headView.setBorderColor(Color.GREEN);
           mBinding.headView.setShadowRadius(UIUtils.dp2px(12));
           mBinding.headView.setShowPressEffect(true);
        */

        //初始化一下权限请求类
        PermissionsUtils.sharedInstance.init(this, "只有同意了才能用哦");
    }


}